//
//  ViewController.m
//  RuntimeDemo
//
//  Created by 张敬 on 2019/10/14.
//  Copyright © 2019年 xiuxiu. All rights reserved.
//

#import "ViewController.h"
#import "UIView+Category.h"
#import "NSObject+Model.h"
#import "NSObject+TestModel.h"
#import "Model.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import "SFS_Thread.h"

@interface Person : NSObject

@end

@implementation Person

//- (void)test{
//    NSLog(@"________ foo");
//}

@end

@interface ViewController ()

@property (nonatomic, strong) SFS_Thread *subThread;


@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self addThread];
}

- (void)setImage{
    UIImage *downloadImage = [UIImage imageNamed:@""];
    UIImageView *imageView = [[UIImageView alloc] init];
    [imageView performSelector:@selector(setImage) withObject:downloadImage afterDelay:0 inModes:@[NSDefaultRunLoopMode]];
}

- (void)addThread{
    
    SFS_Thread *thread = [[SFS_Thread alloc] initWithTarget:self selector:@selector(test) object:nil];
    thread.name = @"张敬";
    [thread start];
    self.subThread = thread;
}

- (void)test{
    
    @autoreleasepool {
//        NSLog(@"________ 线程开始 %@", [NSRunLoop currentRunLoop]);
//        [NSThread sleepForTimeInterval:3];
//        NSLog(@"________ 线程结束 %@", [NSRunLoop currentRunLoop]);
        
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addPort:[NSMachPort port] forMode:NSRunLoopCommonModes];
        
        NSLog(@"启动前RunLoop的Mode： %@", runLoop.currentMode);
        [runLoop run];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self performSelector:@selector(subTheadOperation) onThread:self.subThread withObject:nil waitUntilDone:NO];
}

- (void)subTheadOperation{
    
    NSLog(@"启动RunLoop后--%@",[NSRunLoop currentRunLoop].currentMode);
    NSLog(@"%@----子线程任务开始",[NSThread currentThread]);
    [NSThread sleepForTimeInterval:3.0];
    NSLog(@"%@----子线程任务结束",[NSThread currentThread]);
}

/*
 RunLoop的使用场景
 1.保证线程长时间存活
 2.
 */

@end
